package com.by.basicservice.config;

import com.by.toolkit.utils.OkHttpUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import java.io.IOException;

@Slf4j
@Configuration
@ConditionalOnProperty(name = "dubbo.enableDetailProfiler", havingValue = "true")
public class DubboDetailProfilerConfig {


    @Value("${dubbo.enableDetailProfiler}")
    private String enableDetailProfiler;

    @Value("${dubbo.application.qos-port}")
    private String port;

    @EventListener(ApplicationStartedEvent.class)
    public void enableDetailProfiler() throws IOException {

        if (!Boolean.TRUE.toString().equals(enableDetailProfiler)) {
            log.info("[DubboDetailProfilerConfig][enableDetailProfiler] 性能采样没有开启,enableDetailProfiler:{},port:{}",enableDetailProfiler,port);
            return;
        }
        String reslult = OkHttpUtil.post("http://127.0.0.1" + ":" + port + "/enableDetailProfiler", Maps.newHashMap(), Maps.newHashMap(), Maps.newHashMap(), String.class , OkHttpUtil.getInstance());
        log.info("[DubboDetailProfilerConfig][enableDetailProfiler] 性能采样开启,enableDetailProfiler:{},port:{},reslult:{}",enableDetailProfiler,port,reslult);

    }

}
